<?php

class Application_Model_GeneralizeRule extends Application_Model_DomainObject {

    protected $_id;
    /**
     * 推广Id
     */
    protected $_generalizeId;
    /**
     * 推广对象
     */
    protected $_grantType;
    /**
     * 发放类型
     */
    protected $_generalizeType;
    /**
     * 礼物类型
     */
    protected $_giftType;
    /**
     * 礼物Id
     */
    protected $_giftId;
    /**
     * 数量
     */
    protected $_num;
    /**
     * 已领取
     */
    protected $_receiveNum;
    /**
     * 发放类型
     */
    protected $_receiveType;
    /**
     * 规则最小限制
     */
    protected $_minRule;
    /**
     * 规则最大限制
     */
    protected $_maxRule;
    /**
     * 领取数量
     */
    protected $_ruleNum;
    /**
     * 规则限制
     */
    protected $_ruleLimit;

    /**
    * 国际运单
    */
    const Grant_Way = 1;
    /**
     * 代购订单
     */
    const Grant_Order = 2;
    /**
     * 上门取件
     */
    const Grant_Visiting = 3;
    /**
     * 充值
     */
    const Grant_Recharge = 4;
    /**
     * 分享
     */
    const Grant_Share = 5;
    /**
     * 参与就送礼物
     */
    const GeneralizeType_Participate = 4;
    /**
     * 天天发放礼物
     */
    const GeneralizeType_Day = 1;
    /**
     * 总发放礼物
     */
    const GeneralizeType_Count = 2;
    /**
     * 客服确认发放
     */
    const GeneralizeType_Service = 3;
    /**
     * 优惠券
     */
    const GiftType_Coupon = 1;
    /**
     * 实体礼物
     */
    const GiftType_Store = 2;
    /**
     * 价格折扣
     */
    const GiftType_Discount = 3;
    /**
     * 数量限制
     */
    const RuleLimit_Num = 1;
    /**
     * 总额限制
     */
    const RuleLimit_Sum = 2;
    /**
     * 已付款
     */
    const ReceiveType_Paid = 1;
    /**
     * 受理
     */
    const ReceiveType_Confrimed = 2;
    /**
     * 完成
     */
    const ReceiveType_Achieve = 3;
    
    public static function getGrantAll() {
        return array(
                   self::Grant_Way => "国际运单"
                ,  self::Grant_Order => "代购订单"
                ,  self::Grant_Visiting => "上门取件"
                ,  self::Grant_Recharge => "充值"
                ,  self::Grant_Share => "分享"
        );
    }
    
    public function getGrantName() {
        $grantArr = self::getGrantAll();
        return $grantArr[$this->_grantType];
    }
    
    public static function getGeneralizeTypeAll() {
        return array(
                   self::GeneralizeType_Participate =>"参与就送礼物"
                ,  self::GeneralizeType_Day => "天天发放礼物"
                ,  self::GeneralizeType_Count => "总发放礼物数"
                ,  self::GeneralizeType_Service => "客服确认发放"
        );
    }
    
    public function getGeneralizeTypeName() {
        $arrType = self::getGeneralizeTypeAll();
        return $arrType[$this->_generalizeType];
    }
    
    public static function getGiftTypeAll() {
        return array(
                   self::GiftType_Coupon => "优惠券"
                ,  self::GiftType_Store => "实体礼物"
                ,  self::GiftType_Discount => "折扣"
        );
    }
    
    public function getGiftTypeName() {
        $arrGift = self::getGiftTypeAll();
        return $arrGift[$this->_giftType];
    }
    
    public static function getRuleLimitAll() {
        return array(
                   self::RuleLimit_Num => "数量"
                ,  self::RuleLimit_Sum => "总金额"
        );
    }
    
    public function getRuleLimitName() {
        $arrRuleLimit = self::getRuleLimitAll();
        return $arrRuleLimit[$this->_ruleLimit];
    }
   
    public static function getGiftList($type) {
        $return = false;
        switch ($type) {
            case self::GiftType_Coupon:
                $couponItem = Application_Model_CouponBatch::batchall();
                foreach ($couponItem as $couponVal) {
                    $return[$couponVal->Id] = $couponVal->Name;
                }
                return $return;
                break;
            case self::GiftType_Store:
                $storeSearch = new Application_Model_Filter_StoreItemSearch();
                $storeSearch->userId = "18277";
                $storeItem = Application_Model_StoreItem::getFilterAll($storeSearch);
                foreach ($storeItem as $storeVal) {
                    $return[$storeVal->Id] = $storeVal->ProductName ;
                }
                return $return;
                break;
            case self::GiftType_Discount:
                $return = self::getActivityDiscount();
                return $return;
            default:
                return $return;
                break;
        }
    }
    
    public static function getActivityDiscount() {
        return array(
            "95" => "9.5折"
            ,"90" => "9折"
            ,"85" => "8.5折"
            ,"80" => "8折"
        );
    }
    
    public function getGiftName() {
        switch ($this->_giftType) {
            case self::GiftType_Coupon:
                $couponItem = Application_Model_CouponBatch::load($this->_giftId);
                return $couponItem->Name;
            case self::GiftType_Store:
                $storeItem = Application_Model_StoreItem::load($this->_giftId);
                return $storeItem->productName;
            case self::GiftType_Discount:
                $arrDiscount = self::getActivityDiscount();
                return $arrDiscount[$this->_giftId];
            default:
                return $this->_giftId;
        }
    }
    
    public static function getReceiveTypeAll() {
        return array(
                   self::ReceiveType_Paid => "付款后"
                ,  self::ReceiveType_Confrimed => "受理后"
                ,  self::ReceiveType_Achieve => "完成后"
        );
    }

    public function getReceiveTypeName() {
        $arrReceiveType = self::getReceiveTypeAll();
        return $arrReceiveType[$this->_receiveType];
    }

    public static function addRule($generalizeId,$grantType,$giftType,$giftId,$giftNum,$ruleLimit,$ruleNum,$minRule,$maxRule,$generalizeType)
    {
        for ($index = 0; $index < count($grantType); $index++) {
            if(($giftNum[$index] > 0 || $giftNum[$index] != "") && $minRule[$index] != "" && $maxRule[$index] != "" && ($ruleNum[$index] > 0 || $ruleNum[$index] != "")) {
                $ruleItem = new Application_Model_GeneralizeRule();
                $ruleItem->generalizeId = $generalizeId;
                $ruleItem->grantType = $grantType[$index];
                $ruleItem->generalizeType = $generalizeType[$index];
                $ruleItem->giftType = $giftType[$index];
                $ruleItem->giftId = $giftId[$index];
                $ruleItem->num = $giftNum[$index];
                $ruleItem->ruleLimit = $ruleLimit[$index];
                $ruleItem->ruleNum = $ruleNum[$index];
                $ruleItem->minRule = $minRule[$index];
                $ruleItem->maxRule = $maxRule[$index];
                $ruleItem->receiveNum = 0;
                $ruleItem->save();
            }
        }
        return true;
    }
    
    public static function getRuleByGeneralizeId($generalizeId)
    {
        return self::getDao()->queryAll(array("generalizeId = "=>$generalizeId));
    }
    
    public static function getRuleByGeneralizeIdGrantType($generalizeId,$grantType) {
        return self::getDao()->queryAll(array("generalizeId = "=>$generalizeId," and grantType = " => $grantType));
    }
    
    public static function getRuleByGrantTypeGiftTypeId($grantType,$giftType,$giftId) {
        $where = array();
        $and = "";
        if($grantType != "") {
            $where[$and." grantType = "] = $grantType;
            $and = " and ";
        }
        if($giftType != "") {
            $where[$and." giftType = "] = $giftType;
            $and = " and ";
        }
        if($giftId != "") {
            $where[$and." giftId = "] = $giftId;
            $and = " and ";
        }
        return self::getDao()->queryAll($where);
    }
    
}